package JAVA101_1118;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-11-19
 * Time:16:14
 */
public class TestJDBCUpdate {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改人的id");
        int id = scanner.nextInt();
        //请输入你要修改的名字
        System.out.println("请输入你要修改的名字");
        String name = scanner.next();
        //1.创建DataSourece对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java101_1118?characterEncoding=utf-8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xcb@19980521");

        //2.创建Connection对象
        Connection connection = dataSource.getConnection();

        //书写sql语句
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);

        int ret = statement.executeUpdate();
        if (ret == 1){
            System.out.println("修改成功!");
        }else{
            System.out.println("修改失败!");
        }
        statement.close();
        connection.close();
    }
}
